package com.cloudtech.multidownload;

import android.content.Context;
import android.content.Intent;

import com.cloudtech.multidownload.entitis.FileInfo;
import com.cloudtech.multidownload.service.DownloadService;
import com.cloudtech.multidownload.util.Logger;
import com.cloudtech.multidownload.util.Utils;


public class MultiDownloadManager {
	
	public static boolean startDownloadFile(Context mContext, FileInfo fileInfo){
		if ((mContext == null) || (fileInfo == null))
			return false;
		
		if(!Utils.isNetAvailable(mContext)){
			return false;
		}
		
		if(fileInfo.isEnd()){
			try{
				Intent intent = new Intent(mContext, DownloadService.class);
				intent.setAction(DownloadService.ACTION_START);
				intent.putExtra("fileInfo", fileInfo);
				mContext.startService(intent);
				fileInfo.setEnd(false);
				return true;
			} catch (Exception e) {
			}
		}
		return false;
	}

	public static boolean stopDownloadFile(Context mContext, FileInfo fileInfo){
		if ((mContext == null) || (fileInfo == null))
			return false;
		try{
			Intent intent = new Intent(mContext, DownloadService.class);
			intent.setAction(DownloadService.ACTION_STOP);
			intent.putExtra("fileInfo", fileInfo);
			mContext.startService(intent);
			fileInfo.setEnd(true);
			return true; 
		} catch (Exception e) {
		}
		return false;
	}
	
	public static boolean destory(Context mContext){
		try{
			Intent intent = new Intent(mContext, DownloadService.class);
			intent.setAction(DownloadService.ACTION_DESTORY);
			mContext.startService(intent);
			return true; 
		} catch (Exception e) {
		}
		return false;
	}

	public static void openDebug(boolean isDebug){
		Logger.isDebug = isDebug;
	}

	public static int getFileId(Intent intent){
		return intent.getIntExtra("id", 0);
	}

	public static int getFileFinished(Intent intent){
		return intent.getIntExtra("finished", 0);
	}

	public static FileInfo getFileInfo(Intent intent){
		return (FileInfo)intent.getSerializableExtra("fileInfo");
	}
}
